#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
const int inf = 0x7fffffff;
struct node
{
    int index, cost;
    node(int index, int cost) : index(index), cost(cost){};
    bool operator<(const node b) const { return cost > b.cost; };
};
class Solution
{
public:
    int manhattanDis(vector<int> &pa, vector<int> &pb) { return abs(pa[0] - pb[0]) + abs(pa[1] - pb[1]); }
    priority_queue<node> que;
    int minCostConnectPoints(vector<vector<int>> &points)
    {
        // 建图
        vector<vector<int>> edge(points.size(), vector<int>(points.size(), inf));
        vector<bool> vis(points.size(), false);
        for (int u = 0; u < points.size(); ++u)
        {
            for (int v = u + 1; v < points.size(); ++v)
            {
                int dis = manhattanDis(points[u], points[v]);
                edge[u][v] = edge[v][u] = dis;
            }
        }
        //Prim算法
        int ans = 0;
        que.emplace(0, 0);
        while (que.empty() == false)
        {
            node nd = que.top();
            que.pop();
            if (vis[nd.index] == true)
                continue;
            vis[nd.index] = true;
            ans += nd.cost;
            for (int i = 0; i < points.size(); ++i)
            {
                if (vis[i] == true || i == nd.index)
                    continue;
                que.emplace(i, edge[nd.index][i]);
            }
        }
        return ans;
    }
};